import re
from lxml import etree
import requests
from db_sql import insert_db
from utils import aluminium_url as url, headers, alu0, alu1
from datetime import datetime
from Logs.log import log1
from retrying import retry


@retry(stop_max_attempt_number=3)
def request():
    resp = requests.get(url, headers=headers)
    html = etree.HTML(resp.content.decode())
    return html


def parse(html):
    db_dict = {}
    main_label = html.xpath("//div[@id='priceList']/a")
    for label in main_label:
        low_avg = label.xpath(".//div[@class='panel-body']/div[@class='text-muted']/text()")[0].strip()
        avg_price = low_avg.split("   ")[0]
        db_dict["avg_price"] = re.search(r"\d+", avg_price).group()
        lower_price = low_avg.split("   ")[1]
        db_dict["lower_price"] = re.search(r"\d+", lower_price).group()
        db_dict["higher_price"] = label.xpath(".//div[@class='panel-body']/h2[@class='text-primary']/strong/text()")[0].strip()
        db_dict["unit"] = label.xpath(".//div[@class='panel-body']/div[@class='text-muted']/small/text()")[0].strip().replace("（", "").replace("）", "")
        # sym = label.xpath(".//div[@class='panel-body']/h1/strong/sup/text()")[0].strip()
        db_dict["change_amount"] = label.xpath(".//div[@class='panel-body']/h1/strong/text()")[0].strip()
        db_dict["change_range"] = ""
        price_date = label.xpath(".//div[@class='panel-heading']//timer/text()")[0].strip()
        db_dict["price_date"] = price_date
        price_date = datetime.strptime(price_date, '%Y/%m/%d')
        timestamp = price_date.strftime('%Y%m%d')
        if main_label.index(label) == 0:
            db_dict["id"] = timestamp + alu0
            db_dict["raw_type"] = alu0
        elif main_label.index(label) == 1:
            db_dict["id"] = timestamp + alu1
            db_dict["raw_type"] = alu1
        db_dict["raw_name"] = label.xpath(".//div[@class='panel-heading']//strong/text()")[0].strip() + "铝锭"
        db_dict["source"] = "大沥铝材网"
        db_dict["create_time"] = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
        insert_db(db_dict)


def run():
    try:
        html = request()
        parse(html)
    except Exception as e:
        log1.error(e)
    log1.info("铝锭END!")


if __name__ == '__main__':
    run()